﻿using System;
using System.Collections.Generic;
using System.Linq;
using Cotrobaie.UsersWebService;

namespace Cotrobaie.UserControl
{
    public partial class RezultateRecomandari : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            iucon.web.Controls.ParameterCollection parameters = iucon.web.Controls.ParameterCollection.Instance;

            printinfo(Server.UrlDecode(parameters["UserID"]), Server.UrlDecode(parameters["UserUsername"]), Server.UrlDecode(parameters["UserParola"]), Server.UrlDecode(parameters["categoryID"]),
                Server.UrlDecode(parameters["PriceMin"]), Server.UrlDecode(parameters["PriceMax"]), Server.UrlDecode(parameters["Pg"]),
                Server.UrlDecode(parameters["IntrariPg"]), Server.UrlDecode(parameters["ordonareTitlu"]), Server.UrlDecode(parameters["ordonarePret"]),
                Server.UrlDecode(parameters["ordonareAutor"]), Server.UrlDecode(parameters["ordonareBrand"]), Server.UrlDecode(parameters["ordonareCategorie"]));
        }
        private void printinfo(string UserID, string UserUsername, string UserParola, string categoryID, string PriceMin, string PriceMax, string Pg, string IntrariPg, string ordonareTitlu, string ordonarePret, string ordonareAutor, string ordonareBrand, string ordonareCategorie)
        {
            if (UserID == null || UserUsername == null || UserParola == null)
            { Literal1.Text = "There are no results."; }
            else
            {
                using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
                {
                    var produseNormale = new List<UsersWebService.TipProdus>();

                    {
                        UsersWebService.MesajeProduse result = null;
                        if (Pg == null) Pg = "0";
                        if (IntrariPg == null) IntrariPg = "1000000";
                        if (PriceMin == null) PriceMin = "0";
                        if (PriceMax == null) PriceMax = "1000000000";
                        if (categoryID == null) categoryID = "-1";

                        UsersWebService.Ordonare[] ord = new UsersWebService.Ordonare[5];
                        ord[0] = ordonareTitlu != null ? (ordonareTitlu == "Crescator" ? UsersWebService.Ordonare.Crescator : (ordonareTitlu == "Descreascator" ? UsersWebService.Ordonare.Descreascator : UsersWebService.Ordonare.Deloc)) : UsersWebService.Ordonare.Deloc;
                        ord[1] = ordonarePret != null ? (ordonarePret == "Crescator" ? UsersWebService.Ordonare.Crescator : (ordonarePret == "Descreascator" ? UsersWebService.Ordonare.Descreascator : UsersWebService.Ordonare.Deloc)) : UsersWebService.Ordonare.Deloc;
                        ord[2] = ordonareAutor != null ? (ordonareAutor == "Crescator" ? UsersWebService.Ordonare.Crescator : (ordonareAutor == "Descreascator" ? UsersWebService.Ordonare.Descreascator : UsersWebService.Ordonare.Deloc)) : UsersWebService.Ordonare.Deloc;
                        ord[3] = ordonareBrand != null ? (ordonareBrand == "Crescator" ? UsersWebService.Ordonare.Crescator : (ordonareBrand == "Descreascator" ? UsersWebService.Ordonare.Descreascator : UsersWebService.Ordonare.Deloc)) : UsersWebService.Ordonare.Deloc;
                        ord[4] = ordonareCategorie != null ? (ordonareCategorie == "Crescator" ? UsersWebService.Ordonare.Crescator : (ordonareCategorie == "Descreascator" ? UsersWebService.Ordonare.Descreascator : UsersWebService.Ordonare.Deloc)) : UsersWebService.Ordonare.Deloc;
                        byte b = 0; foreach (var o in ord) if (o != UsersWebService.Ordonare.Deloc) b++;

                        result = serviciu.GetUsersRecommendations3(int.Parse(UserID),UserUsername,UserParola, int.Parse(Pg), int.Parse(IntrariPg), double.Parse(PriceMin), double.Parse(PriceMax),
                            int.Parse(categoryID), ord[0], ord[1], ord[2], ord[3], ord[4], b);

                        if (result.Success)
                            produseNormale = result.Produse.ToList();
                    }
                    if (produseNormale.Count() == 0)
                    { Literal1.Text = "There are no results."; }
                    else
                    {
                        PlaceHolder1.InnerHtml = String.Empty;
                        PlaceHolder1.InnerHtml += Cotrobaie.Clase.TabelSearchuri.tabelProducts(produseNormale);
                    }
                    Search.produsePagina = produseNormale.Count();
                }
            }
        }
    }
}